package com.onpositive.datamap.core.impl;

import java.util.Arrays;

import com.onpositive.datamap.core.IDataProperty;
import com.onpositive.datamap.values.impl.ValueSequence;
import com.onpositive.semantic.model.api.meta.IMeta;
import com.onpositive.semantic.model.api.property.FunctionOperator;
import com.onpositive.semantic.model.api.property.IFunction;
import com.onpositive.semantic.model.groups.BasicGroupingOperators;

public class IdentityGroupingCalculator extends FunctionOperator {

	@Override
	protected Object calc(final IFunction f, Object o2) {
		
		return BasicGroupingOperators.getInstance().calc(new IFunction() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public IMeta getMeta() {
				return null;
			}
			ValueSequence nullSeq=new ValueSequence((IDataProperty) f, "<none>", new String[]{""});
			
			@Override
			public Object getValue(Object context) {
				
				Object value = f.getValue(context);
				if (value==null){
					return nullSeq;
				}
				if(value instanceof ValueSequence){
					ValueSequence s=(ValueSequence) value;
					Object[] parts = s.getParts();
					Object[] np=new Object[parts.length];
					int a=0;
					for (Object o:parts){
						
						np[a++]=new ValueSequence(s.getProperty(), "", new String[]{o.toString()});
					}
					return Arrays.asList(np);
				}
				return value;
			}
		},o2);
	}

}
